//
//  StudiesView.m
//  AoJi
//
//  Created by IMacLife on 15/2/3.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//
//我的留学

#import "StudiesView.h"

@implementation StudiesView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadView];
        [self loadData];
    }
    return self;
}

//加载view
-(void)loadView
{
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(5, 5, Mainwidth-10, self.frame.size.height-5) style:UITableViewStylePlain];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.backgroundColor=[UIColor clearColor];
    
    //_tableview.separatorStyle=UITableViewCellAccessoryNone;
    [self addSubview:_tableview];
    
}

-(void)loadData
{
//    Url：http://cent.dsgrd.com/me/study
    [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:nil WithApi:@"me/study" WhenSuccess:^(NSMutableDictionary *response) {
        NSLog(@"%@",response);
        studyArray = response;
        [_tableview reloadData];
        
        // status 0 已经完成 1 正在进行 2还没开始
        NSLog(@"%@",studyArray[0][@"adviser"]);
        NSLog(@"%@",studyArray[0][@"substeps"][0][@"title"]);
        NSLog(@"%@",studyArray[0][@"title"]);
    } WhenFailure:^(NSError *failure) {
        NSLog(@"%@",failure);
    }];
}

#pragma mark ----------------UITableViewDelegate
//有几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return studyArray.count;
}

//一个分区显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [studyArray[section][@"substeps"]count];
}

//自定义section  title
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
    headerView.backgroundColor=[UIColor whiteColor];
    
    UILabel *left = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100, 40)];
    left.text = studyArray[section][@"title"];
    left.font = [UIFont systemFontOfSize:17];
    [headerView addSubview:left];
    
    UILabel *right = [[UILabel alloc]initWithFrame:CGRectZero];
    NSString *str = @"顾问";
    NSString *rightStr = [NSString stringWithFormat:@"%@%@:%@",studyArray[section][@"title"],str,studyArray[section][@"adviser"]];
    right.text=rightStr;
    right.font = [UIFont systemFontOfSize:12];
    [right sizeToFit];
    right.frame = CGRectMake(tableView.frame.size.width-right.frame.size.width-5, 20, right.frame.size.width, right.frame.size.height);
    [headerView addSubview:right];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 49, tableView.frame.size.width, 1)];
    line.backgroundColor=[Color colorFromHexRGB:@"f0f0f0"];
    [headerView addSubview:line];
    
    switch (section) {
        case 0:
            left.textColor=[Color colorFromHexRGB:@"f96004"];
            break;
            
        case 1:
            left.textColor=[Color colorFromHexRGB:@"3ec37d"];
            break;
            
        case 2:
            left.textColor=[Color colorFromHexRGB:@"48abea"];
            break;
            
        default:
            break;
    }
    
    return headerView;
}

//section 的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

//设置section的间距
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

//UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSArray *substeps = studyArray[indexPath.section][@"substeps"];
    cell.textLabel.text = substeps[indexPath.row][@"title"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *status = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width-60, 10, 50, 20)];
    status.textColor=[UIColor whiteColor];
    status.textAlignment=NSTextAlignmentCenter;
    status.font = [UIFont systemFontOfSize:12];
    
    switch ([substeps[indexPath.row][@"status"]intValue]) {
        case 0:
            status.text=@"已完成";
            status.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"yiwancheng"]];
            break;
        case 1:
            status.text=@"进行中";
            status.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"jingxingzjhong"]];
            break;
        case 2:
            status.text=@"未进行";
            status.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"weiwancheng"]];
            break;
        
        default:
            break;
    }
    [cell.contentView addSubview:status];
    
    return cell;
}


//cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}



@end
